/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.shindig.factories;

import com.shop.opensocial.mediated.types.GadgetContextMediator;
import com.shop.opensocial.mediated.types.SecurityTokenMediator;
import com.shop.opensocial.mediated.types.UserPrefsMediator;
import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.UserPrefs;
import java.util.Locale;

/**
 * Factory for converting between Mediator type and Shindig type
 *
 * @author Jordan Zimmerman
 */
public class GadgetContextMediatorFactory extends GenericMediatorFactory<GadgetContext, GadgetContextMediator>
{
	public static final GenericMediatorFactoryBase<GadgetContext, GadgetContextMediator> 	i = new GadgetContextMediatorFactory();

	@Override
	protected GadgetContext internalMake(final GadgetContextMediator c)
	{
		final SecurityTokenMediator		token = c.getToken();
		return new GadgetContext()
		{
			@Override
			public String getParameter(String name)
			{
				return c.getParameter(name);
			}

			@Override
			public Uri getUrl()
			{
				return Uri.parse(c.getGadgetURL());
			}

			@Override
			public int getModuleId()
			{
				return c.getModuleId();
			}

			@Override
			public Locale getLocale()
			{
				return c.getLocale();
			}

			@Override
			public boolean getIgnoreCache()
			{
				return c.getIgnoreCache();
			}

			@Override
			public String getHost()
			{
				return c.getHost();
			}

			@Override
			public String getUserIp()
			{
				return c.getUserIp();
			}

			@Override
			public boolean getDebug()
			{
				return c.getDebug();
			}

			@Override
			public String getView()
			{
				String v = c.getView();
				return (v != null) ? v : super.getView();
			}

			@Override
			public UserPrefs getUserPrefs()
			{
				UserPrefsMediator p = c.getUserPrefs();
				return (p != null) ? UserPrefsMediatorFactory.i.make(p) : super.getUserPrefs();
			}

			@Override
			public SecurityToken getToken()
			{
				return SecurityTokenMediatorFactory.i.make(token);
			}
		};
	}

	@Override
	protected GadgetContextMediator internalUnmake(final GadgetContext c)
	{
		return new GadgetContextMediator()
		{
			@Override
			public String getParameter(String name)
			{
				return c.getParameter(name);
			}

			@Override
			public String getGadgetURL()
			{
				return c.getUrl().toString();
			}

			@Override
			public Locale getLocale()
			{
				return c.getLocale();
			}

			@Override
			public boolean getIgnoreCache()
			{
				return c.getIgnoreCache();
			}

			@Override
			public String getHost()
			{
				return c.getHost();
			}

			@Override
			public String getUserIp()
			{
				return c.getUserIp();
			}

			@Override
			public boolean getDebug()
			{
				return c.getDebug();
			}

			@Override
			public String getView()
			{
				return c.getView();
			}

			@Override
			public int getModuleId()
			{
				return c.getModuleId();
			}

			@Override
			public UserPrefsMediator getUserPrefs()
			{
				return UserPrefsMediatorFactory.i.unmake(c.getUserPrefs());
			}

			@Override
			public SecurityTokenMediator getToken()
			{
				return SecurityTokenMediatorFactory.i.unmake(c.getToken());
			}
		};
	}

	private GadgetContextMediatorFactory()
	{
	}
}
